package com.cswangdao.controller.admin;


import com.cswangdao.domain.dto.CouponPageDTO;
import com.cswangdao.domain.dto.CouponUserPageDTO;
import com.cswangdao.domain.model.MarketCouponUser;
import com.cswangdao.domain.po.Coupon;
import com.cswangdao.result.PageResult;
import com.cswangdao.result.Result;
import com.cswangdao.service.CouponService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RequestMapping("/admin/coupon")
@RestController
@Slf4j
public class AdminCouponController {

    @Autowired
    private CouponService couponService;

    /**
     * @description:优惠卷分页查询
     * @param: request, response
     * @return: void
     */
    @GetMapping("/list")
    public Result<PageResult<Coupon>> list(CouponPageDTO couponPageDTO) {
        log.info("优惠卷信息分页查询：{}", couponPageDTO);
        PageResult<Coupon> pageResult = couponService.pageQueryInfo(couponPageDTO);
        return Result.success(pageResult);
    }

    /**
     * @description: 新增优惠卷
     * @param: request, response
     * @return: void
     */
    @PostMapping("/create")
    public Result<Coupon> create(@RequestBody Coupon coupon) {
        log.info("新增优惠卷：{}", coupon);
        couponService.addCoupon(coupon);

        return Result.success(coupon);
    }

    /**
     * @description: 修改优惠卷信息
     * @param: request, response
     * @return: void
     */
    @PostMapping("/update")
    public Result<Coupon> update(@RequestBody Coupon coupon) {
        log.info("修改优惠卷信息：{}", coupon);
        couponService.updateById(coupon);
        return Result.success(coupon);
    }


    /**
     * @description: 删除优惠卷信息
     * @param: request, response
     * @return: void
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Coupon coupon) {
        log.info("删除优惠卷信息：{}", coupon);
        couponService.deleteById(coupon.getId());
        return Result.success();
    }


    /**
     * @description: 查询优惠卷与用户之间的信息
     * @param: request, response
     * @return: void
     */
    @GetMapping("/listuser")
    public Result<PageResult<MarketCouponUser>> listuser(CouponUserPageDTO pageDTO) {
        log.info("查询优惠卷与用户之间的信息：{}", pageDTO);
        PageResult<MarketCouponUser> pageResult = couponService.getCouponUserList(pageDTO);
        return Result.success(pageResult);
    }

    // TODO read详情
    @GetMapping("/read")
    public Result<Coupon> read(Integer id) {
        log.info("查询优惠卷详情：{}", id);
        Coupon coupon = couponService.queryById(id);
        return Result.success(coupon);
    }
}
